package git.soulbgm.cookies;

import git.soulbgm.utils.JsonUtil;
import git.soulbgm.utils.PropertiesUtil;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * @author SoulBGM
 * @version 1.0
 * @date 2021/6/14 18:42
 */
public class CookiesApplication {

    private static WebDriver driver;
    private static String passportJdUrl = "https://passport.bilibili.com/login";
    private static String jdUrl = "https://www.bilibili.com/";
    private static String cookiesFile = "D:\\cookies.txt";

    /**
     * 判断运行时是否是以jar运行
     *
     * @return boolean
     */
    private static boolean isJar(String path) {
        File file = new File(path);
        return file.isFile();
    }

    /**
     * 检测环境
     */
    private static void detectionEnvironment() throws IOException {
        String path = CookiesApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String configFilePath = "config.properties";
        if (isJar(path)) {
            path = path.substring(0, path.lastIndexOf(PropertiesUtil.LEFT_SLANTING_BAR));
            configFilePath = path + File.separator + "config.properties";
        }
        Map<String, String> config = PropertiesUtil.getPrefixProperties(configFilePath, "config", "#");
        System.setProperty("webdriver.chrome.driver", config.get("chromeDriverPath"));
    }

    /**
     * 得到文件
     *
     * @param driver 司机
     * @return {@link Document}
     */
    private static Document getDocument(WebDriver driver) {
        String htmlContent = driver.getPageSource();
        return Jsoup.parse(htmlContent);
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        detectionEnvironment();
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--mute-audio");
        chromeOptions.addArguments("--hide-scrollbars");
        driver = new ChromeDriver(chromeOptions);
        getCookies();
    }

    private static void getCookies() throws IOException {
        driver.get(passportJdUrl);
        Scanner scanner = new Scanner(System.in);
        String result = scanner.nextLine();
        if ("ok".equalsIgnoreCase(result)) {
            Set<Cookie> cookies = driver.manage().getCookies();
            String json = JsonUtil.toJson(cookies);
            OutputStream os = new FileOutputStream(cookiesFile);
            IOUtils.write(json, os, StandardCharsets.UTF_8);
            driver.close();
        }
    }

    private static void login() throws IOException {
        driver.get(jdUrl);
        InputStream is = new FileInputStream(new File(cookiesFile));
        String json = IOUtils.toString(is, StandardCharsets.UTF_8);
        List<Cookie> cookies = JsonUtil.parseArray(json, Cookie.class);
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                driver.manage().addCookie(cookie);
            }
        }
        driver.get(jdUrl);
    }

}
